package com.zt.teacher.controller;

import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.Teachers;
import com.zt.common.pojo.common.JsonResult;
import com.zt.common.pojo.netty.Sender;
import com.zt.common.utils.RedisUtil;
import com.zt.common.utils.UserUtil;
import com.zt.teacher.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * description:
 * author:zt
 * date:2021-12-07
 */

@RestController
@RequestMapping("teacher")
@CrossOrigin
@Api("教师模块")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @PostMapping("login")
    @ApiOperation("登录接口")
    public JsonResult<Sender> login(@RequestBody Teachers teacher, HttpServletResponse response){

        return JsonResult.success(teacherService.login(teacher,response));
    }

    @GetMapping("/{password}/{salt}")
    public JsonResult<String> register(@PathVariable String password,@PathVariable String salt){
        String pwd = new Md5Hash(password, salt, 1024).toHex();
        return JsonResult.success(pwd);
    }

    @GetMapping("check")
    @ApiOperation("检查token状态")
    public JsonResult check(HttpServletRequest request){
        if (UserUtil.checkUserStatus(request)) {
            return JsonResult.success();
        }
        else return JsonResult.error(CodeMsg.VERIFY_ERROR);
    }

    @PostMapping("logOut")
    @ApiOperation("退出登录")
    public JsonResult logOut(HttpServletRequest request){
        teacherService.logOut(request);
        return JsonResult.success();
    }

    @PostMapping("getIdentity")
    @ApiOperation("获取教师身份是否管理员")
    public JsonResult<Boolean> getIdentity(HttpServletRequest request){
        return JsonResult.success(teacherService.getIdentity(request));
    }
}
